package com.wu.kthnode;


/**
 * 二叉搜索树的第K个节点
 *
 * @author lynn
 * @date 2020/8/30 - 20:32
 */

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    private TreeNode res;
    private int index;

    TreeNode KthNode(TreeNode pRoot, int k) {

        index = k;
        getNode(pRoot);
        return res;
    }

    private void getNode(TreeNode pRoot) {
        if (pRoot == null || index < 0) {
            return;
        }
        getNode(pRoot.left);
        index--;
        if (index == 0) {
            res = pRoot;
            return;
        }
        getNode(pRoot.right);
    }
}